package org.liaohailong.library.image.victor;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.lang.ref.WeakReference;

/**
 * Author: liaohailong
 * Date: 2019/7/14
 * Time: 10:50
 * Description: 网络图片处理框架
 * 1，任务编队管控
 * 2，gif播放 依赖 koral--/android-gif-drawable
 * ------------------------ implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.17'
 * 3，图片仅下载
 * 4，获取图片本地缓存路径
 *
 * @author liaohailong
 */
public enum Avatar {
    /**
     * 全局单例
     */
    INSTANCE;

    private static final String TAG = "Avatar";
    private WeakReference<Context> contextWeakReference;

    private AvatarCache avatarCache = new AvatarCache();
    private AvatarManager avatarManager = new AvatarManager();

    public void init(Context context, String savePath) {
        init(context, savePath, AvatarCache.DEFAULT_MAX_CACHE_SIZE);
    }

    public void init(Context context, String savePath, int maxCacheSize) {
        contextWeakReference = new WeakReference<>(context);
        avatarCache.setSavePath(savePath);
        avatarCache.setMaxCacheSize(maxCacheSize);
    }

    AvatarCache getAvatarCache() {
        return avatarCache;
    }

    public Context getContext() {
        return contextWeakReference.get();
    }

    @MainThread
    public void displayImage(@Nullable String tag, @NonNull String url,
                             @NonNull ImageView imageView, int width, int height,
                             @Nullable Drawable placeHolder,
                             @Nullable Drawable error,
                             @Nullable OnImageCallback callback) {
        Context context = contextWeakReference.get();
        tag = tag == null || tag.length() == 0 ? context != null ? context.toString() : TAG : tag;
        avatarManager.execute(tag, url, imageView, width, height, placeHolder, error, callback);
    }

    @MainThread
    public void onlyDownload(@Nullable String tag, @NonNull String url, int width, int height, @Nullable OnImageCallback callback) {
        Context context = contextWeakReference.get();
        tag = tag == null || tag.length() == 0 ? context != null ? context.toString() : TAG : tag;
        avatarManager.execute(tag, url, null, width, height, null, null, callback);
    }

    public void stopRequests(Context context) {
        avatarManager.stopRequests(context.toString());
    }

    public void resumeRequests(Context context) {
        avatarManager.resumeRequests(context.toString());
    }

    public void stopRequests(String tag) {
        avatarManager.stopRequests(tag);
    }

    public void resumeRequests(String tag) {
        avatarManager.resumeRequests(tag);
    }

    public void pauseGif(String tag) {
        avatarManager.pauseGif(tag);
    }

    public void resumeGif(String tag) {
        avatarManager.resumeGif(tag);
    }

    public boolean isCache(String url) {
        return avatarCache.isCached(url);
    }

    public String getCachePath(String url) {
        return avatarCache.getFilePath(url);
    }

    /**
     * 网络图片回调接口
     */
    public interface OnImageCallback {
        /**
         * displayImage方法回调
         *
         * @param success  true表示成功 false表示失败
         * @param drawable 成功后的资源drawable 这里有可能是BitmapDrawable也有可能是GifDrawable
         */
        void onDisplayImageComplete(boolean success, @Nullable Drawable drawable);


        /**
         * onlyDownload方法回调
         *
         * @param success true表示成功 false表示失败
         * @param path    本地缓存路径
         */
        void onDownloadImageComplete(boolean success, @Nullable String path);
    }
}
